package com.cy.pj.health.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//@Controller
//public class HealthController {//Handler对象(通过此对象处理DispatcherServlet
//    @RequestMapping("/doHealth")
//    public String doHealth(){
//        return "default";
//    }
//}
//@Controller
//public class HealthController {//Handler对象(通过此对象处理DispatcherServlet
//    @RequestMapping("/doHealth")
//    public ModelAndView doHealth(){
//        ModelAndView mv = new ModelAndView();
//        mv.setViewName("default");
//        mv.addObject("username","陈梓树");
//        mv.addObject("state","亚健康");
//        return mv;
//    }
//}
//@Controller
//public class HealthController {//Handler对象(通过此对象处理DispatcherServlet
//    @RequestMapping("/doHealth")
//    public String doHealth(Model model){
//        ModelAndView mv = new ModelAndView();
//        model.addAttribute("username", "jason");
//        model.addAttribute("state", true);
//        return "default";
//    }
//}
@Controller
public class HealthController {//Handler对象(通过此对象处理DispatcherServlet
    @RequestMapping("/doHealthJson")
    @ResponseBody
    public Map<String,Object> doHealth(Model model){
        Map<String, Object> map = new HashMap<>();
        map.put("xiao", "小");
        map.put("da", "大");
        return map;

    }

    @RequestMapping("/doPrint")
    @ResponseBody
    public void doPring(HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("xiao", "小");
        map.put("da", "大");
        //转化为json
        ObjectMapper om = new ObjectMapper();
        String jsonStr = om.writeValueAsString(map);
        System.out.println("jsonStr=" + jsonStr);
        //响应到客户端
        response.setCharacterEncoding("utf-8");
        response.getWriter().println(jsonStr);
    }
}

